#ifndef __NET_H_
#define __NET_H_

#include <netinet/in.h>
#include <string>
#include <memory>
#include <set>

using namespace std;
/* 通信函数 */

#define CharsToInt(A)    (long) (((int) ((unsigned char) (A)[0])) +\
                            (((int) ((unsigned char) (A)[1])) << 8) +\
                            (((int) ((unsigned char) (A)[2])) << 16) +\
                            (((long) ((unsigned char) (A)[3])) << 24))


#define IntToChars(T,A)    {  *((T))=(unsigned char) ((A));\
                            *((T)+1)=(unsigned char) (((A) >> 8));\
                            *((T)+2)=(unsigned char) (((A) >> 16));\
                            *((T)+3)=(unsigned char) (((A) >> 24)); }

class NetWork
{
public:
    static int NetSplitStr(char* inStr, char cc, int maxLen, int iArr[]);
    static int NetMkdirP(char* errStr, const char* path, int mode);

    static int NetMakeSocket(char* errStr, int port);
    static int NetAcceptSocket(char* errStr, int socket, string& clientIp, int block);
    static int NetConnect(char* errStr, int port, const string& host, int timeOut);
    static int NetClose(int socket);

    static int NetGetIpList(char* ipList, int len);
    static int NetGetIdlePort(char* errStr);
    static int NetSendData(char* errStr, int nSocket, char* buf, int bufLen);
    static int NetRecvData(char* errStr, int nSocket, char* buf, int timeOut);

    static int NetSendConfirm(char* errStr, int nSocket);
    static int NetWaitConfirm(char* errStr, int nSocket);
    
    static int NetSetSendBuf(char* errStr, int nSocket, int bufLen);
    static int NetSetRecvBuf(char* errStr, int nSocket, int bufLen);

    static int NetReadSocketTm(char* errStr, int nSocket, char* readBuf, int readSize, int timeOut);
    static int NetReadSocket(char* errStr, int nSocket, char* readBuf, int readSize);

    static int NetReadSocketNoBlock(char* errStr, int nSocket, char* readBuf, int readSize, int timeOut);
    static int NetReadSocketOne(char* errStr, int nSocket, char* readBuf, int readSize);

    static int NetWriteSocket(char* errStr, int nSocket, const char* writeBuf, int writeSize);
    static int NetCheckSocket(char* errStr, int nSocket);

    static int AnetKeepAlive(char* errStr, int fd);
    static int NetError(char* errStr, const char* const preStr, int errNum);
    static int NetTimeOut(char* errStr, int socket, int timeOut/* ms */);
    static int NetSetSendTimeo(char* errStr, int nSocket);
    static int NetSetNoDelay(char* errStr, int nSocket);

};
#endif